package com.cloud.core.widget.dialog;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.StringRes;

import com.cloud.core.R;


/**
 * Created by ZiLlion
 * date：2018/4/17
 * desc: 重要信息提示对话框
 */

public class AlertInfoDialog extends CommonDialog {
    private TextView tvDialogContent;
    private boolean isShowLeft = true;
    protected TextView btn_cancel, btn_confirm;


    public AlertInfoDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int resId() {
        return R.layout.layout_alert;
    }

    @Override
    protected void initView() {
        super.initView();
        tvDialogContent = mView.findViewById(R.id.tv_dialog_content);
    }

    @Override
    protected void initEvent() {
        btn_cancel = mView.findViewById(R.id.btn_cancel);
        btn_confirm = mView.findViewById(R.id.btn_confirm);
        btn_confirm.setOnClickListener(v -> {
            if (clickListener != null) {
                clickListener.onRightClick();
            }
            AlertInfoDialog.this.dismiss();
        });

        btn_cancel.setOnClickListener(v -> {
            if (clickListener != null) {
                clickListener.onLeftClick();
            }
            AlertInfoDialog.this.dismiss();
        });
        btn_cancel.setVisibility(isShowLeft ? View.VISIBLE : View.GONE);
    }

    public void setShowLeft(boolean showLeft) {
        btn_cancel.setVisibility(showLeft ? View.VISIBLE : View.GONE);
    }

    private void setContent(String content) {
        tvDialogContent.setText(content);
    }

    private void setLeftText(String leftText) {
        btn_cancel.setText(leftText);
    }

    private void setRightText(String rightText) {
        btn_confirm.setText(rightText);
    }

    private void setClickListener(OnClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public static class Builder {

        private final Context context;
        private String content;
        private String leftText = "取消";
        private String rightText = "确定";
        private OnClickListener clickListener;
        private boolean cancelable = true;
        private boolean isShowLeft = true;

        public Builder(@NonNull Context context) {
            this.context = context;
        }

        public Builder setContent(@StringRes int contentId) {
            content = context.getString(contentId);
            return this;
        }

        public Builder setContent(String content) {
            this.content = content;
            return this;
        }

        public Builder setLeftText(@StringRes int textId) {
            leftText = context.getString(textId);
            return this;
        }

        public Builder setLeftText(String text) {
            leftText = text;
            return this;
        }

        public Builder setRightText(@StringRes int textId) {
            rightText = context.getString(textId);
            return this;
        }

        public Builder setRightText(String text) {
            rightText = text;
            return this;
        }

        public Builder setClickListener(OnClickListener clickListener) {
            this.clickListener = clickListener;
            return this;
        }

        public Builder setCancelable(boolean cancelable) {
            this.cancelable = cancelable;
            return this;
        }

        public Builder setShowLeft(boolean showLeft) {
            this.isShowLeft = showLeft;
            return this;
        }

        public AlertInfoDialog show() {
            AlertInfoDialog alertInfoDialog = new AlertInfoDialog(context);
            alertInfoDialog.show();
            alertInfoDialog.setClickListener(clickListener);
            alertInfoDialog.setContent(content);
            alertInfoDialog.setLeftText(leftText);
            alertInfoDialog.setRightText(rightText);
            alertInfoDialog.setCancelable(cancelable);
            alertInfoDialog.setShowLeft(isShowLeft);
            alertInfoDialog.setCanceledOnTouchOutside(cancelable);
            return alertInfoDialog;
        }
    }
}
